package com.example.shiro.controller;

import com.example.shiro.session.TokenWebSessionManager;
import com.example.shiro.utils.JWTUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class AuthController {
    @PostMapping("login")
    public Object login(HttpServletRequest request){
        SecurityUtils.getSubject().login(new UsernamePasswordToken("user","123456"));
        return request.getAttribute(TokenWebSessionManager.SESSION_ID_TOKEN_NAME);
    }


    /**
     * jwt登录，没有报错则登录成功，返回JWT加密后的串
     * @param request
     * @return
     */
    @PostMapping("jwt/login")
    public Object jwtLogin(HttpServletRequest request){
        SecurityUtils.getSubject().login(new UsernamePasswordToken("user","123456"));

        Map<String,String> py = new HashMap<>();
        py.put("user","admin");

        return JWTUtils.getToken(py);
    }
}
